package com.jet.chat.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Arguments {
	private static final Pattern pattern = Pattern.compile("^-(.*)$");
	private HashMap<String, Argument> arguments = new HashMap<String, Argument>();
	
	private void createArgument(String name) {
		this.arguments.put(name, new Argument(new ArrayList<String>()));
	}
	
	public Argument getArgument (String key) {
		return arguments.get(key);
	}
	
	public Arguments(String[] arg) {
		int i = -1;
		Matcher matcher;
		if (arg.length > 0) {
			while (i < arg.length - 1) {
				i = i + 1;
				matcher = pattern.matcher(arg[i]);
				if (matcher.matches()) {
					String name = matcher.group(1);
					createArgument(name);
					i = i + 1;
					matcher = pattern.matcher(arg[i]);
					while (i < arg.length && !(matcher = pattern.matcher(arg[i])).matches()) {
						arguments.get(name).addValue(arg[i]);
						i = i + 1;
					}
				} else {
					createArgument(arg[i]);
				}
			}
		}
	}
	
	public String getValue(String name) {
		Argument argument = arguments.get(name);
		if (argument == null) {
			return null;
		} else {
			return argument.getValue();
		}
	}
	
	public void list() {
		System.out.println("listing begins");
		for (String key : arguments.keySet()) {
			System.out.println("key: " + key);
			for (String value : arguments.get(key).getValues()) {
				System.out.println("\t" + value);
			}
		}
		System.out.println("listing ends");
	}
}
